﻿using System.Windows;
using System.Windows.Media;
using C1.Silverlight.Pdf;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public static class HouseMapMileageScale
    {

        public static void Draw(C1PdfDocument pdf, PDFPoint scaleCursor, double scaleBorderWidth, double scaleBorderHeight)
        {
            // draw border 
            Pen mileageScalePen = new Pen(Colors.Black, 1.0);

            double barHeight = scaleBorderHeight * .1;
            double barUnitWidth = (scaleBorderWidth * .8) / 3;  // temp width, need to pass in scale based on map
            double adjustableMargin = scaleBorderWidth * .1;  // temp margin, need to adjust based on barUnitWidth


            DrawBars(pdf, scaleCursor, mileageScalePen, barUnitWidth, adjustableMargin, barHeight);
        }

        private static void DrawBars(C1PdfDocument pdf, PDFPoint scaleCursor, Pen mileageScalePen, double barUnitWidth, double adjustableMargin, double barHeight)
        {
            PDFPoint barCursor = new PDFPoint(scaleCursor);

            // move the bars down below where the text should go
            barCursor.AddX(adjustableMargin);
            barCursor.AddY(barHeight + barHeight * .5);

            // draw top bars starting with black
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth / 2, barHeight, barCursor, true);
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth / 2, barHeight, barCursor, false);

            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth, barHeight, barCursor, true);
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth, barHeight, barCursor, false);

            barCursor.x = scaleCursor.x;
            barCursor.AddX(adjustableMargin);
            barCursor.AddY(barHeight);

            // draw bottom bars starting with white
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth / 2, barHeight, barCursor, false);
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth / 2, barHeight, barCursor, true);

            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth, barHeight, barCursor, false);
            barCursor = DrawUnitBar(pdf, mileageScalePen, barUnitWidth, barHeight, barCursor, true);
        }

        private static PDFPoint DrawUnitBar(C1PdfDocument pdf, Pen mileageScalePen, double barWidth, double barHeight, PDFPoint barCursor, bool isFilled)
        {
            if (isFilled)
            {
                pdf.FillRectangle(Colors.Black, barCursor.x, barCursor.y, barWidth, barHeight);
            }
            pdf.DrawRectangle(mileageScalePen, barCursor.x, barCursor.y, barWidth , barHeight);
            barCursor.AddX(barWidth);
            return barCursor;
        }

        //public static void DrawLabels(C1PdfDocument pdf, PDFPoint scaleCursor)
        //{

        //    double barUnitMileage = 10;
        //    double barMileage = barUnitMileage;

        //    PDFString barMileageText = new PDFString(pdf, "Arial", 12, PdfFontStyle.Regular, Colors.Black, barMileage.ToString(), 1);
        //    barMileageText.FitFontSize(barUnitWidth, barHeight);

        //    PDFPoint barCursor = new PDFPoint(scaleCursor);
        //    barCursor.AddX(adjustableMargin);
        //    barCursor.AddY(scaleBorderHeight * .25);

        //    DrawUnitLabel(barMileageText, barCursor, barMileage);

        //    barMileageText.text = (Math.Round(barMileage / 2, 0)).ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);

        //    barMileageText.text = (0).ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);

        //    barMileageText.text = barMileage.ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);

        //    barMileageText.text = (barMileage * 2).ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);

        //    barMileageText.text = (barMileage * 3).ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);

        //    barMileageText.text = (barMileage * 4).ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);

        //}

        //private static void DrawUnitLabel(PDFPoint barCursor, PDFString barMileageText, double barMileage )
        //{
        //    barMileageText.text = barMileage.ToString();
        //    barMileageText.Draw(barCursor.x - barMileageText.textBoxSize.Width / 2, barCursor.y, barUnitWidth, barHeight);
        //    barCursor.AddX(barUnitWidth);
        //}

    }
}
